//
//  MainViewController.swift
//  SinaWeiBo
//
//  Created by tuyue on 16/3/5.
//  Copyright © 2016年 xueyang.co.ltd. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    //MARK: 按钮监听事件
    //unrecognized selector sent to instance 0x7fdeb2d10040  找不到该方法
    //1.不希望外界访问改方法 可以加上 private关键字 来限制访问
    //2.一旦targe - action 对应的方法添加了private 之后 运行循环就无法找到该方法
    //3.在方法前添加 @objc 告诉运行循环 这个方法是基于OC消息动态派发机制来执行
    /**plusBtn的点击事件方法*/
    @objc private func plusBtnClick(){
     /**创建控制器*/
        let compose = ComposeViewController()
        /***/
        let nav = UINavigationController(rootViewController: compose)
        
        presentViewController(nav, animated: true, completion: nil)
    }
    
    
    //MARK: - viewDidLoad()
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let mainTabbar = MainTabBar()
        /**将自定义的mianTabbar替代系统的tabBar*/
        setValue(mainTabbar, forKey: "tabBar")
        
        /**创建常量，保存plusBtn的点击方法名*/
        let actionName = "plusBtnClick"
        
        /**给plusBtn添加一个点击事件*/
        mainTabbar.plusBtn.addTarget(self, action: Selector(actionName), forControlEvents: .TouchUpInside)
        
        /**调取添加子控制器的方法*/
        addChildViewControllers()
        
    
    }
    
    
    
    //MARK:添加子视图控制器
    private func addChildViewControllers(){
        addChildViewController(HomeViewController(), vcTitleName: "首页", imageName: "tabbar_home")
        addChildViewController(MessageViewController(), vcTitleName: "消息中心", imageName: "tabbar_message_center")
        addChildViewController(DiscoverViewController(), vcTitleName: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileViewController(), vcTitleName: "个人中心", imageName: "tabbar_profile")
    }
    
    
    //MARK: - 创建私有方法来添加子控制器
    private func addChildViewController(vc: UIViewController , vcTitleName: String ,imageName: String) {
        
//        tabBar.tintColor = UIColor.orangeColor()
        
        let nav = UINavigationController(rootViewController: vc)
        
        vc.title = vcTitleName
        
        vc.tabBarItem.image = UIImage(named: imageName)
        //
        vc.tabBarItem.selectedImage = UIImage(named:  imageName+"_selected")
        
        
        
        /**将创建的子控制器添加到tabBarViewController*/
        addChildViewController(nav)
    }
    
    
    
    



}
